<template>
  <div>
    <el-dialog
      :title="mode==='add'?'新增分组':'编辑分组'"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="false"
        :model="form"
        :rules="rules"
        label-width="130px"
        size="small"
      >
        <el-form-item label="分组名称：" prop="title">
          <el-input v-model.trim="form.title" maxlength="4" show-word-limit />
        </el-form-item>
        <el-form-item label="分组排序：" prop="ranking">
          <el-input v-model="form.ranking" maxlength="7" placeholder="数字越大排序越靠前" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :disabled="loading" round @click="hide">取 消</el-button>
        <el-button size="small" :disabled="loading" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addShelfCategory,
  updateShelfCategory
} from '@/views/wxaMallModule/api/shelf'
import { validateCustomPositiveInt } from '@/utils/validate.js'
export default {
  data() {
    // 校验方法
    const validatorRanking = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入排序'))
      } else if (!validateCustomPositiveInt(value, 7)) {
        callback(new Error('请输入正整数(数字越大排序越靠前)'))
      } else {
        callback()
      }
    }
    return {
      // 校验字段
      rules: {
        title: [
          {
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change']
          }
        ],
        ranking: [
          {
            required: true,
            validator: validatorRanking,
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      mode: 'add',
      form: {
        ranking: null,
        title: null,
        shelfList: [],
        type: 'LOCAL'
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ mode = add, row = null }) {
      this.mode = mode
      if (mode == 'edit') {
        this.form = JSON.parse(JSON.stringify(row))
      }
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form = {
        ranking: null,
        title: null,
        shelfList: [],
        type: 'LOCAL'
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = this.form
          this.loading = true
          if (this.form.id) {
            updateShelfCategory(params)
              .then((res) => {
                this.$notify({
                  title: '成功',
                  message: '更新成功',
                  type: 'success',
                  duration: 2000,
                  offset: 40
                })
                this.resolve && this.resolve()
                this.hide()
              })
              .catch((err) => {})
              .finally(() => {
                this.loading = false
              })
          } else {
            addShelfCategory(params)
              .then((res) => {
                this.$notify({
                  title: '成功',
                  message: '新增成功',
                  type: 'success',
                  duration: 2000,
                  offset: 40
                })
                this.resolve && this.resolve()
                this.hide()
              })
              .catch((err) => {})
              .finally(() => {
                this.loading = false
              })
          }
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
